/**
 * 
 */
package com.beta.restapi.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.beta.common.model.ErrorResponse;
import com.beta.common.web.AbstractBaseController;
import com.beta.em.UserType;
import com.beta.entity.Couriers;
import com.beta.entity.Users;
import com.beta.restapi.internal.Consts;
import com.beta.vo.PageVO;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author vector
 *
 */
public abstract class BaseController extends AbstractBaseController {

	@Autowired
	protected HttpServletRequest request;
	
	@Autowired
	protected HttpServletResponse response;


	/**
	 * @return 当前登录用户(只在用户类型为 大B 和 小B 时调用)
	 * @see #getCurrentCourier
	 */
	public Users getCurrentUser() {
		return (Users) request.getAttribute(Consts.RequestAttributeKey.USER_OBJ);
	}

	/**
	 * @return 用户为承运人时，返回承运人对象
	 */
	public Couriers getCurrentCourier() {
		return (Couriers) request.getAttribute(Consts.RequestAttributeKey.USER_OBJ);
	}

	public Integer getCurrentUserId() {
		return getCurrentUserId(request);
	}

	public UserType getCurrentUserType() {
		return getCurrentUserType(request);
	}

	public Integer getCurrentUserId(HttpServletRequest request) {
		Object currentUser = request.getAttribute(Consts.RequestAttributeKey.USER_OBJ);
		if (currentUser instanceof Users) {
			return ((Users) currentUser).getId();
		} else if (currentUser instanceof Couriers) {
			return ((Couriers) currentUser).getId();
		} else {
			return null;
		}
	}

	public UserType getCurrentUserType(HttpServletRequest request) {
		Object currentUser = request.getAttribute(Consts.RequestAttributeKey.USER_OBJ);
		if (currentUser instanceof Users) {
			return UserType.getUserType(((Users) currentUser).getUserType());
		} else if (currentUser instanceof  Couriers) {
			return UserType.COURIER;
		} else {
			return null;
		}
	}

	public String getMessage(String code) {
		return new RequestContext(request).getMessage(code);
	}

//	/**
//	 * 当后端服务端返回{@link MessageResult}时，直接调用这个方法作为controller的返回值
//	 *
//	 * @param messageResult
//	 * @return
//	 */
//	public ResponseEntity<?> response(MessageResult messageResult) {
//		String code = messageResult.getCode();
//		if (StringUtils.startsWith(code, "S")) {
//			return ok(messageResult.getResult());
//		} else if (StringUtils.startsWith(code, "E")) {
//			return error(code);
//		} else {
//			return error(code);
//		}
//	}

	public <T> ResponseEntity<T> ok() {
		return ResponseEntity.ok().body(null);
	}
	
	public <T> ResponseEntity<T> ok(T data) {
		return ResponseEntity.ok().body(data);
	}

	public <T> ResponseEntity<T> ok(int httpStatus, T data) {
		return ResponseEntity.status(httpStatus).body(data);
	}

	public ResponseEntity<?> error(String code) {
		return error(HttpStatus.SC_UNPROCESSABLE_ENTITY, code);
	}
	
	public ResponseEntity<?> error(int httpStatus, String code) {
		return error(httpStatus, code, getMessage(code));
	}

	public ResponseEntity<?> error(int httpStatus, String code, String message) {
		return ResponseEntity.status(httpStatus).body(new ErrorResponse(code, message));
	}

	public <T> void fillPageParam(Page<T> page, PageVO pageVO) {
		pageVO.setTotal(page.getTotal());
		pageVO.setTotalPage(page.getPages());
		int current = page.getCurrent();
		pageVO.setPage(current);
		if (page.hasNext()) {
			pageVO.setNextPage(current + 1);
		}
		if (page.hasPrevious()) {
			pageVO.setPrevPage(current - 1);
		}
	}

}
